package com.example.wenqujingdian;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Process;

import com.example.wenqujingdian.api.ApiConfig;
import com.example.wenqujingdian.utils.Lsetting;
import com.example.wenqujingdian.utils.ToastUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.vise.xsnow.http.ViseHttp;

import java.util.List;


/**
 * Created by 29083 on 2018/3/28.
 */

public class MyApplication extends Application {

    public static MyApplication mContext;

//    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        String processName = getProcessName(Process.myPid());
        if (!processName.endsWith("remote")){
            ViseHttp.init(this);
            //配置请求主机地址

            ViseHttp.CONFIG().baseUrl(ApiConfig.ORG_URL);

//            setupNetworkListener(true);
//            GreenDaoHelper helper = new GreenDaoHelper(this,"book.db",null);
//            DaoMaster daoMaster = new DaoMaster(helper.getWritableDb());
//            daoSession = daoMaster.newSession();
        }
        //判断app是否是debug
        if (isApkInDebug(mContext)) {
            Lsetting.debug = true;
            Lsetting.sdCardPath = mContext.getFilesDir().getPath();
            ToastUtil.show("当前为debug模式");
        }
        CrashReport.initCrashReport(getApplicationContext(), "36f6bd3c00", false);
    }


    /**
     * 判断当前应用是否是debug状态
     */
    public static boolean isApkInDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }


    private String getProcessName(int pid) {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        if (am == null) return null;
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        if (processInfos == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return null;
    }

//    private void setupNetworkListener(boolean on) {
//        if (on) {
//            NetWorkUtils.getInstance().startConnectionTrack(this);
//        } else {
//            NetWorkUtils.getInstance().stopConnectionTrack(this);
//        }
//    }
}
